package com.kate.cloud.shiro.web;

import com.kate.cloud.shiro.util.IpUtils;
import com.kate.cloud.shiro.util.ResultVOUtil;
import com.kate.cloud.shiro.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 李金照 on 2017/12/13.
 *
 * @author 金照
 */
@RestController
@Slf4j
@Api(value = "登录接口", description = "登录接口")

public class LoginController {

    @ApiOperation(value = "用户登录")
    @PostMapping(value = "/login")
    public ResultVO<String> login(@RequestParam String username,@RequestParam String password, HttpServletRequest request){
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            return ResultVOUtil.error("账号或密码不为空");
        }
        String clientIp = IpUtils.getRemoteAddr(request);

        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(username,password,clientIp);
           token.setRememberMe(true);

            try {
                subject.login(token);
            }catch (UnknownAccountException uae) {
                // 若没有指定的账户, 则 shiro 将会抛出 UnknownAccountException 异常.
                log.info("----> There is no user with username of " + token.getPrincipal());
                return ResultVOUtil.error("账号不存在");
            }catch (IncorrectCredentialsException ice) {
                // 若账户存在, 但密码不匹配, 则 shiro 会抛出 IncorrectCredentialsException 异常。
                log.info("----> Password for account " + token.getPrincipal() + " was incorrect!");
                return ResultVOUtil.error("密码错误");
            }catch (LockedAccountException lae) {
                // 用户被锁定的异常 LockedAccountException
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
                return ResultVOUtil.error("账号被锁定");
            }catch (AuthenticationException ae) {
                // 所有认证时异常的父类.
                return ResultVOUtil.error("登录失败");
            }
        }
        return ResultVOUtil.success("登录成功");
    }

    @GetMapping("/login")
    public ModelAndView login(){
        return new ModelAndView("login");
    }

    @GetMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("index");;
        mv.addObject("name","KATE");
        return mv;
    }

    @GetMapping("/user")
    public ModelAndView user(){
        return new ModelAndView("user");
    }

    @GetMapping("/admin")
    public ModelAndView admin(){
        return new ModelAndView("admin");
    }

    @GetMapping("/403")
    public ModelAndView unAuthorization(){
        return new ModelAndView("403");
    }

}
